package com.example.nuc_ms_beta.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.nuc_ms_beta.R;
import com.example.nuc_ms_beta.app.MyApplication;
import com.example.nuc_ms_beta.util.SharePreferencesUtil;

import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.UpdateListener;


/**
 * Created by bnhc on 17-6-3.
 * Regist for Action Use SMS
 */

public class RegistActivity extends Activity implements View.OnClickListener {

    private Button activity_regist_send_bt, activity_regist_ok_bt, activity_regist_finsh;

    private EditText activity_regist_phone_et, activity_regist_code_et;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        initView();
    }



    private void initView() {
        activity_regist_send_bt = (Button) findViewById(R.id.activity_regist_send_bt);
        activity_regist_ok_bt = (Button) findViewById(R.id.activity_regist_ok_bt);
        activity_regist_finsh = (Button) findViewById(R.id.activity_regist_finsh);
        activity_regist_phone_et = (EditText) findViewById(R.id.activity_regist_phone_et);
        activity_regist_code_et = (EditText) findViewById(R.id.activity_regist_code_et);
        activity_regist_send_bt.setOnClickListener(this);
        activity_regist_ok_bt.setOnClickListener(this);
        activity_regist_finsh.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_regist_ok_bt:
                if(activity_regist_ok_bt.getText().toString().equals("")){
                    Toast.makeText(MyApplication.context,"验证码为空",Toast.LENGTH_SHORT).show();
                }else{
                    BmobSMS.verifySmsCode(activity_regist_phone_et.getText().toString(), activity_regist_code_et.getText().toString(), new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                           if(e==null){
                               Toast.makeText(MyApplication.context,"验证码正确",Toast.LENGTH_SHORT).show();
                               SharePreferencesUtil.saveData(MyApplication.context,"UserPhone",(String)activity_regist_phone_et.getText().toString());
                               Intent completeIntent = new Intent(RegistActivity.this,CompleteInformationActiviy.class);
                               startActivity(completeIntent);
                               finish();
                           }else{
                               Toast.makeText(MyApplication.context,"验证码错误",Toast.LENGTH_SHORT).show();
                           }
                        }
                    });
                }
                break;
            case R.id.activity_regist_send_bt:

                BmobSMS.requestSMSCode(activity_regist_phone_et.getText().toString(), "Test", new QueryListener<Integer>() {
                    @Override
                    public void done(Integer integer, BmobException e) {
                      if(e==null){
                          Toast.makeText(MyApplication.context,"获取短信成功",Toast.LENGTH_SHORT).show();
                      }
                    }
                });
                break;
            case R.id.activity_regist_finsh:
                finish();
                break;
        }
    }

}


